﻿#include "SceneToScene.h"
#include "../DevMacros.h" 



void SceneToScene::setAllScene(  map<int, CMapDoorConf*>* doors   ){
    if(doors==NULL){
        return;
    }
    if(!m_gAllSceneVo.empty()){
        m_gAllSceneVo.clear();
    }
    
    map<int, CMapDoorConf*>::iterator it=doors->begin();
    
    for( ;it!=doors->end() ;it++){
        m_gAllSceneVo.push_back(it->second);
    } 
}



void SceneToScene::findDoor(int   tsid){
    
    if(nTargetId == tsid){
        return;
    }else{	//寻找下一个场景
        if(nIndex >= m_gAllDoor.size()){
            m_gFindDoorArr=NULL;
            return;
        }
      
        vector<CMapDoorInfoConf*>* vec= m_gAllDoor[nIndex];
        int doorSize=vec->size();
        
        
        nTargetId =(*vec)[doorSize-1]->nTSid;
        m_gFindDoorArr = vec;
        m_pStartScene=NULL;
        nIndex++;
        vector<CMapDoorConf*>::iterator it= m_gOpenScene.begin();
        for(;it!=m_gOpenScene.end();it++){
            if((*it)->nScendId == nTargetId){
                m_pStartScene = (*it);
                m_gOpenScene.erase(it);
                break;
            }
        }
        //创建路径
        if(m_pStartScene){
            
            for(int j=0;j<m_pStartScene->gDoorList.size();j++){
                vector<CMapDoorInfoConf*>* newVec =new vector<CMapDoorInfoConf*>() ;
                
                for(uint32 i=0;i< vec->size();i++){
                    newVec->push_back((*vec)[i]);
                } 
                newVec->push_back(m_pStartScene->gDoorList[j]); 
                m_gAllDoor.push_back(newVec);
            }
            
            			
        }
    }			 
    findDoor(tsid);
    
}

void SceneToScene::find(int fSceneId,int tSceneId, vector<CMapDoorInfoConf>& ret){
    
    ret.clear();
    m_pStartScene=NULL;
    m_gFindDoorArr=NULL;
    
    if(!m_gCloseScene.empty()){
        m_gCloseScene.clear();
    }
    
    if(!m_gOpenScene.empty()){
        m_gOpenScene.clear();
    }
    
    if( !m_gAllSceneVo.empty()){
        
        m_gOpenScene.insert(m_gOpenScene.end(), m_gAllSceneVo.begin(), m_gAllSceneVo.end());
    }
    
    if(!m_gAllDoor.empty()){
        for(uint32 i=0;i<m_gAllDoor.size();i++){
            SAFE_DEL(m_gAllDoor[i]);
        }
        m_gAllDoor.clear();
    }
    
    
    vector<CMapDoorConf*>::iterator it= m_gOpenScene.begin();
  
    for(;it!=m_gOpenScene.end();it++){ 
        if((*it)->nScendId == fSceneId){
            m_pStartScene = (*it);
            m_gOpenScene.erase(it);
            break;
        }
    }
    if(m_pStartScene==NULL){
        Trace("找不到开始的场景");
        return ;
    }
    if(fSceneId == tSceneId){
        Trace("在本场景，无需要寻找");
        return ;
    }
    
    for(int j=0;j<m_pStartScene->gDoorList.size();j++){
        vector<CMapDoorInfoConf*>* vec =new vector<CMapDoorInfoConf*>();
        vec->push_back(m_pStartScene->gDoorList[j]);
        m_gAllDoor.push_back(vec);
    }
    
    
    nIndex = 0;
    nTargetId = fSceneId;
    findDoor(tSceneId);
    
    if(m_gFindDoorArr){
        for(uint32 i=0;i<m_gFindDoorArr->size();i++){
             
            CMapDoorInfoConf item;
            CMapDoorInfoConf* old =(*m_gFindDoorArr)[i];
            old->clone(&item); 
            ret.push_back(item);
        }
    }
    
    if(!m_gAllDoor.empty()){
        for(uint32 i=0;i<m_gAllDoor.size();i++){
            SAFE_DEL(m_gAllDoor[i]);
        }
        m_gAllDoor.clear();
    }
    return ;
}


 